﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WHM3.addNewEmployee
{
    public partial class addNewEmployeePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginDetails details = (LoginDetails)Session["myobject"];
            try
            {
                if (BL.getEmployeeByLoginDetails(details).isAdmin.Equals("False"))
                {
                    //hack attempt
                    Response.Redirect("~/General/unAuthorized.aspx");
                }
            }
            catch
            {
                //hack attempt
                Response.Redirect("~/General/unAuthorized.aspx");
            }

            //hide login button
            Control h = this.Header.FindControl("LoginView1");
            h.Visible = false;

            //load 'problem' label
            if (Session["problem"] != null)
            {
                this.problem.Text = (string)Session["problem"];
                this.problem.Visible = true;
            }
        }

        protected void addToDB(object sender, EventArgs e)
        {
            try
            {
                Employee emp = new Employee(this.ID.Text, this.firstName.Text, this.lastName.Text, this.ID.Text, this.Position.Text, this.isManager.Checked.ToString(), this.isAdmin.Checked.ToString(), this.Team.Text, this.Team.Text,this.minHours.Text);
                if (BL.addEmployeeToDB(emp))
                {
                    //added succesfully
                    this.problem.Text = emp.id + " Was added succesfully";
                    
                    Session["problem"] = this.problem.Text;
                    Response.Redirect("~/addNewEmployee/addNewEmployeePage.aspx");
                }
                else
                {
                    this.problem.Text = "something went wrong...";
                    
                    Session["problem"] = this.problem.Text;
                    Response.Redirect("~/addNewEmployee/addNewEmployeePage.aspx");
                }
            }
            catch
            {
                //there was a problem connecting to DB
                this.problem.Text = "There was a problem connecting to the DB";
                
                Session["problem"] = this.problem.Text;
                Response.Redirect("~/addNewEmployee/addNewEmployeePage.aspx");
            }
        }
    }
}